﻿using System;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;

namespace ch.froorider.NGoogleCalendar.tools
{
	/// <summary>
	/// General excpetion class for this application. This class extends the common application
	/// exception with a field called MessageCode. This field can be used 
	/// to "fill" the exception with a code instead of a common message string.
	/// 
	/// Created to fulfill rule CA2201.
	/// </summary>
	[Serializable()]
	public class NGoogleCalendarException : Exception
	{
		private int messageCode;

		#region Constructors

		/// <summary>
		/// Default constructor. Needed see CA1032
		/// </summary>
		public NGoogleCalendarException()
		{
		}

		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="message">A message according to this exception</param>
		public NGoogleCalendarException(string message) : base(message)
		{
		}

		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="message">A message according to this exception</param>
		/// <param name="innerException">The further exception you caught.</param>
		public NGoogleCalendarException(string message, Exception innerException)
			: base(message, innerException)
		{
		}

		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="messageCode">The message code to this exception</param>
		public NGoogleCalendarException(int messageCode)
		{
			this.messageCode = messageCode;
		}

		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="message">A message according to this exception</param>
		/// <param name="messageCode">The message code to this exception</param>
		public NGoogleCalendarException(string message, int messageCode) : base(message)
		{
			this.messageCode = messageCode;
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="info">Stores all the data needed to serialize or deserialize an object.</param>
		/// <param name="context">Describes the source and destination of a given serialized stream.</param>
		protected NGoogleCalendarException(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
		}

		#endregion

		#region Properties

		/// <summary>
		/// Property. Gives you the message code stored in this exception.
		/// </summary>
		public int MessageCode
		{
			get { return this.messageCode; }
		}

		#endregion

		#region public methods

		/// <summary>
		/// Formats the exception content as a string. The string uses the InvariantCulture.
		/// </summary>
		/// <returns>A formatted string containing the message.</returns>
		public override string ToString()
		{
			StringBuilder builder = new StringBuilder();
			builder.AppendLine("NGoogleCalendarException");
			builder.AppendLine("Message: " + base.Message);
			builder.AppendLine("Message code: " + this.messageCode);
			builder.AppendLine("Base exception: "+base.ToString());
			return builder.ToString();
		}

		/// <summary>
		/// Populates a SerializationInfo with the data needed to serialize the target object.
		/// </summary>
		/// <param name="info">Stores all the data needed to serialize or deserialize an object.</param>
		/// <param name="context">Describes the source and destination of a given serialized stream.</param>
		[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);
			if (info == null)
			{
				throw new ArgumentNullException("info");
			}
			else
			{
				info.AddValue("MessageCode", this.messageCode);
			}
		}

		#endregion
	}
}
